﻿

<?php
$gameUrl=$_GET["gUrl"];//"http://voxcast.oyunlar1.com/games/6085.swf";
$userId=$_GET["uId"];//"123456";  
$gameName=$_GET["gName"];//"Abluka";
$picUrl=$_GET["pUrl"];//"games/Savas_Games/Abluka.jpg";
$gameDesc=$_GET["gDesc"];//"None";
$listId=$_GET["lName"];//"www";


require('_config-rating.php'); // get the db connection info

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
}


$sql="SELECT gameId FROM games WHERE gameUrl = '".$gameUrl."'";
$result = mysql_query($sql,$con);
$row = mysql_fetch_array($result);

if ($row["gameId"]>0){//eger oyun game tablosunda varsa
	$gameId = $row["gameId"];
	/*
	$sql="SELECT gameListId FROM gamelist WHERE userId = $userId and gameListId = $listId";
	add2log($con,"sql = ".$sql);
	$result = mysql_query($sql,$con);
	$row = mysql_fetch_array($result);
	if ($row["gameListId"]>0){//eger oyun gamelist tablosunda varsa*/
		
		//$gameListId = $row["gameListId"];	
		add2log($con,$listId);
		$sql="INSERT INTO listgamerelations (listId, gameId) VALUES ('$listId','$gameId')";
		if (!mysql_query($sql,$con))
		{
		  echo false;
		}else echo true;
	//}
}
else {//eger oyun games tablosunda yoksa
	$sql="INSERT INTO games (picUrl, gameUrl, gameName,gameDesc) VALUES ('$picUrl','$gameUrl','$gameName','$gameDesc')";
	add2log($con,'test3');
	if (!mysql_query($sql,$con))
	{
		echo false;
	}else{
		$sql="SELECT gameId FROM games WHERE gameUrl = '$gameUrl'";
		$result = mysql_query($sql,$con);
		$row = mysql_fetch_array($result);
		if ($row["gameId"]>0){//eger oyun game tablosunda varsa
			$gameId = $row["gameId"];
			
			$sql="SELECT gameListId FROM gamelist WHERE userId = $userId and gameListName = '$listName'";
			$result = mysql_query($sql,$con);
			$row = mysql_fetch_array($result);
			if ($row["gameListId"]>0){//eger oyun gamelist tablosunda varsa
			
				$gameListId = $row["gameListId"];
					
				$sql="INSERT INTO listgamerelations (listId, gameId) VALUES ('$gameListId','$gameId')";
				if (!mysql_query($sql,$con))
				{
				  echo false;
				}else echo true;
			}
		}else echo false;

	}

}

/*$sql="INSERT INTO games (picUrl, gameUrl, gameName,gameDesc) VALUES ('$picUrl','$gameUrl','$gameName','$gameDesc')";

if (!mysql_query($sql,$con))
  {
  //die('Error: ' . mysql_error());
  echo "false";
  }else echo "true";
*/
mysql_close($con);


/*mysql_select_db("gamer", $con);

//$sql="SELECT * FROM user WHERE id = '".$q."'";

$sql="insert into games (picUrl, gameUrl, gameName,gameDesc) values ('','denem','deneme','denem','deneme')";

$result = mysql_query($sql);
echo $result;
if (!$result){
echo 'query returned results';
}
else {
echo 'query returned no results';
}

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "<td>" . $row['Age'] . "</td>";
  echo "<td>" . $row['Hometown'] . "</td>";
  echo "<td>" . $row['Job'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);

echo "Favorilerinize eklendi.".$gameName."-".$gameUrl."-".$picUrl;
*/

function add2log($con,$str){
	$logger = "insert into logger (Id, log) values (null,'$str' )";
	$result = mysql_query($logger,$con);
	//0$row = mysql_fetch_array($result);
	$err = mysql_error($con);
}
?>